package com.hudson.plugin.server.hibernateDB.internal.domain.dao;

import com.hudson.plugin.server.hibernateDB.domain.dao.CoverageDAOInterface;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.CoverageEntity;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Coverage entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .Coverage
 */

public class CoverageDAO extends HibernateDaoSupport implements CoverageDAOInterface {
    private static final Logger log = LoggerFactory
            .getLogger(CoverageDAO.class);

    protected void initDao() {
        // do nothing
    }

    public void save(CoverageEntity transientInstance) {
        log.debug("saving Coverage instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(CoverageEntity persistentInstance) {
        log.debug("deleting Coverage instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public CoverageEntity findById(Integer id) {
        log.debug("getting Coverage instance with id: " + id);
        try {
            CoverageEntity instance = (CoverageEntity) getHibernateTemplate().load(
                    CoverageEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(CoverageEntity instance) {
        log.debug("finding Coverage instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Coverage instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from CoverageEntity as model where model."
                    + propertyName + " = ?";
            List results = getHibernateTemplate().find(queryString, value);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByRdFuncCov(Object rdFuncCov) {
        return findByProperty(RD_FUNC_COV, rdFuncCov);
    }

    public List findByQaFuncCov(Object qaFuncCov) {
        return findByProperty(QA_FUNC_COV, qaFuncCov);
    }

    public List findByRdCdCov(Object rdCdCov) {
        return findByProperty(RD_CD_COV, rdCdCov);
    }

    public List findByQaCdCov(Object qaCdCov) {
        return findByProperty(QA_CD_COV, qaCdCov);
    }

    public List findByRdLineCov(Object rdLineCov) {
        return findByProperty(RD_LINE_COV, rdLineCov);
    }

    public List findByQaLineCov(Object qaLineCov) {
        return findByProperty(QA_LINE_COV, qaLineCov);
    }

    public List findAll() {
        log.debug("finding all Coverage instances");
        try {
            String queryString = "from CoverageEntity";
            List results = getHibernateTemplate().find(queryString);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public CoverageEntity merge(CoverageEntity detachedInstance) {
        log.debug("merging Coverage instance");
        try {
            CoverageEntity result = (CoverageEntity) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(CoverageEntity instance) {
        log.debug("attaching dirty Coverage instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(CoverageEntity instance) {
        log.debug("attaching clean Coverage instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static CoverageDAOInterface getFromApplicationContext(ApplicationContext ctx) {
        return (CoverageDAOInterface) ctx.getBean("CoverageDAO");
    }
}